package com.uchiha.nova.mainService.wrapper;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uchiha.nova.mainService.service.IServiceService;
import com.uchiha.nova.core.model.entity.ServiceEntity;
import com.uchiha.nova.core.model.vo.ServiceVO;
import com.uchiha.nova.core.tool.node.ForestNodeMerger;
import com.uchiha.nova.core.tool.support.BaseEntityWrapper;
import com.uchiha.nova.core.tool.util.SpringUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * controller表  包装类,返回视图层所需的字段
 *
 * @author Chill
 */
public class ServiceWrapper extends BaseEntityWrapper<ServiceEntity, ServiceVO>  {

    IServiceService serviceService = SpringUtil.getBean(IServiceService.class);

	public static ServiceWrapper build() {
		return new ServiceWrapper();
 	}

	@Override
	public ServiceVO entityVO(ServiceEntity service) {
		ServiceVO serviceVO = new ServiceVO();
		BeanUtil.copyProperties(service, serviceVO);
		Long a = serviceService.count(new QueryWrapper<ServiceEntity>().eq("parent_id",service.getId()));
		if(a.compareTo(0L)==1){
			serviceVO.setHasChildren(true);
		}else{
			serviceVO.setHasChildren(false);
		}
		return serviceVO;
	}
	public List<ServiceVO> listNodeVO(List<ServiceEntity> list) {
		List<ServiceVO> collect = list.stream().map(service -> {
			ServiceVO serviceVO = new ServiceVO();
			BeanUtil.copyProperties(service, serviceVO);
			Long a = serviceService.count(new QueryWrapper<ServiceEntity>().eq("parent_id",service.getId()));
			if(a.compareTo(0L)==1){
				serviceVO.setHasChildren(true);
			}else{
				serviceVO.setHasChildren(false);
			}
			return serviceVO;
		}).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}

	@Override
	public IPage<ServiceVO> pageVO(IPage<ServiceEntity> pages) {
		IPage<ServiceVO> page = pages.convert(item->{
			ServiceVO serviceVO = new ServiceVO();
			BeanUtil.copyProperties(item, serviceVO);
			Long a = serviceService.count(new QueryWrapper<ServiceEntity>().eq("parent_id",item.getId()));
			if(a.compareTo(0L)==1){
				serviceVO.setHasChildren(true);
			}else{
				serviceVO.setHasChildren(false);
			}
			return serviceVO;
		});
		return page;
	}
}
